package codeRandomThoughts.Test101对称二叉树;

import codeRandomThoughts.utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 迭代法
 */
public class Solution2 {
    public boolean isSymmetric(TreeNode root) {
        if (root == null)
            return true;
        //这不是层序遍历,而是用一个容器来成对的存放我们要比较的元素,所以其实用什么都可以
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);

        while (!queue.isEmpty()) {
            TreeNode right = queue.poll();
            TreeNode left = queue.poll();
            if (right == null && left == null) {
                continue;//对称
            }
            if (right == null || left == null || left.val != right.val) {
                return false;
            }

            queue.offer(left.left);
            queue.offer(right.right);
            queue.offer(left.right);
            queue.offer(right.left);
        }
        //一定是要遍历完整个才知道是不是对称
        return true;
    }
}
